package org.colcon.web.client.ui;

import org.colcon.web.client.Collective_Congress;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Terminal extends ColConPanel 
{
	private VerticalPanel panel;
	
	private ScrollPanel scroll;
	private VerticalPanel output;
	
	private HorizontalPanel bar;
	private TextBox input;
	private Button button;
	
	public Terminal(Collective_Congress client) 
	{
		super(client);
		
		panel = new VerticalPanel();
		output = new VerticalPanel();
		scroll = new ScrollPanel(output);
		
		bar = new HorizontalPanel();
		input = new TextBox();
		button = new Button("Enter");
		
		panel.setWidth("100%");
		scroll.setHeight("500px");
		
		bar.setWidth("100%");
		input.setWidth("90%");
		button.setWidth("90%");
		
		input.setStyleName("input");
		
		bar.add(input);
		bar.add(button);
		
		panel.add(scroll);
		panel.add(bar);
		
		add(panel);
		
		output.add(new HTML("<pre>~ welcome to collective congress ~</pre>", true));
		output.add(new HTML(
				"<pre><a href=\"http://code.collectivecongress.com/\">Google Code</a></pre>" +
				"<pre><a href=\"http://group.collectivecongress.com\">Google Groups</a></pre>" +
				"<pre><a href=\"http://rcos.collectivecongress.com\">RCOS project page</a></pre>" +
				"<pre><a href=\"emailto:danvegeto@gmail.com\">danvegeto@gmail.com</a></pre>"
		));
		output.add(new HTML("<pre>enter 'help' for a list of commands</pre>", true));
		
		button.addClickHandler(new MyButtonHandler());
		
		sinkEvents(Event.ONKEYPRESS);
		addHandler(new MyEnterHandler(), KeyPressEvent.getType());
		
		setFocus(input);
	}
	
	private class MyButtonHandler implements ClickHandler
	{
		public void onClick(ClickEvent event) 
		{
			buttonClick();
		}
	}
	
	private class MyEnterHandler implements KeyPressHandler
	{
		public void onKeyPress(KeyPressEvent event) 
		{
			if(event.getCharCode() == KeyCodes.KEY_ENTER)
				if(button.isEnabled())
					buttonClick();
		}
	}

	public void buttonClick()
	{
		output.add(new HTML("<pre> > " + input.getText() + "</pre>"));
		button.setEnabled(false);
		
		getService().execute(input.getText(), new AsyncCallback<String>()
		{
			public void onFailure(Throwable caught)
			{
				caught.printStackTrace();
			}
			
			public void onSuccess(String str)
			{
				if(str != null)
				{
					output.add(new HTML("<pre>" + str + "</pre>", true));
				}
				else
				{
					output.add(new HTML("<pre>command '" + input.getText() + "' failed</pre>", true));
				}
				
				scroll.scrollToBottom();
				input.setText("");
				setFocus(input);
				button.setEnabled(true);
			}
		});
	}
}
